<?php	
class player 
{
	var $playerCode;
	var $playerName;
	var $playerPosition;
	var $playerValue;
	var $playerTeam;
	var $playerWeekIn;
	var $playerWeekOut;
	var $playerScore;
	var $weeklyScore = array();
	var $playerActive;
	var $playerLastYearScore;
	var $maxWeeks;
	var $captains;
	var $In;
	var $Out;
	var $Current;
	
	function player($playerCode)
	{
		$cDatabase = new database();

		$connect = $cDatabase->connectToDatabase();
		$activeDatabase = $cDatabase->getActiveDatabase($connect);
	
		$db = $cDatabase->selectDatabase($connect, $activeDatabase);

		$this->maxWeeks = $cDatabase->GetMaxWeeks($connect);
    
		$this->captains = $cDatabase->GetCaptainsEnabled($connect);
    
		$error = 0;
		// playerCodes can be 3 or 4 in length depending on the Daily Telegraph year
		// For 2007-8, the Telegraph went from 3 length codes to 4 length codes
		if ( ($length = strlen($playerCode) ) != 3 && ($length = strlen($playerCode) ) != 4)
		{
			echo"<BR>Error detected for Player Code: expected length 3 or 4 received length ".$length;
			$error = 1;
		}
		else
			$this->playerCode = $playerCode;
		return $error;
	}

	//	Functions to add player attributes into the class from the form, do the checks and then put into the database
	function addPlayerName($playerName)
	{
		$error = 0;
		if ( ($length = strlen($playerName) ) > 30)
		{
			echo"<BR>Error detected for Player Name:  length exceeds 30 received length ".$length;
			$error = 1;
		}
		else
			$this->playerName = $playerName;
		return $error;
	}
	function addPlayerActive($playerActive)
	{
		$error = 0;
		if ( ($length = strlen($playerActive) ) > 1)
		{
			echo"<BR>Error detected for Player Active:  length exceeds 1 received length ".$length;
			$error = 1;
		}
		else
			$this->playerActive = $playerActive;
		return $error;
	}
	function addPlayerTeam($playerTeam)
	{
		$error = 0;
		if ( ($length = strlen($playerTeam) ) != 3)
		{
			echo"<BR>Error detected for Team Name: expected length 3 received length ".$length;
			$error = 1;
		}
		else
			$this->playerTeam = $playerTeam;
		return $error;
	}
	function addPlayerValue($playerValue)
	{
		$error = 0;
		$playerValue = round($playerValue, 1);		// Round to nearest decimal place
		if ( ($playerValue < 1.0) || ($playerValue > 99.9) )
		{
			echo"<BR>Error detected for Player Value: value range 1.0m to 99.9m received value ".$playerValue."m";
			$error = 1;
		}
		else
			$this->playerValue = $playerValue;
		return $error;
	}
	function addPlayerPosition($playerPosition)
	{
		$error = 0;
		if ( ($playerPosition != 'GK') &&  
			($playerPosition != 'DEF') && 
			($playerPosition != 'MID') && 
			($playerPosition != 'ATT') )
		{
			echo"<BR>Error detected for Player Position: expected either GK, DEF, MID or ATT received value ".$playerPosition;
			$error = 1;
		}
		else
			$this->playerPosition = $playerPosition;
		return $error;
	}
	function addPlayerLastYearScore($playerLastYearScore)
	{
		$error = 0;
		if (/*($playerLastYearScore < 0) ||*/ ($playerLastYearScore > 999))
		{
			echo"<BR>Error detected for Player Last Year Score received value ".$playerLastYearScore;
			$error = 1;
		}
		else
			$this->playerLastYearScore = $playerLastYearScore;
		return $error;
	}
	
	// Functions to set the player attributes from the database
	function setPlayerName($connect)
	{
		$query = "SELECT * FROM playerlist WHERE playerCode='".$this->playerCode."'";
		$result = mysqli_query($connect, $query);
		$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
		$this->playerName = $row['playerName'];
		
		return 0;
	}
	function setPlayerActive($connect)
	{
		$query = "SELECT * FROM playerlist WHERE playerCode='".$this->playerCode."'";
		$result = mysqli_query($connect, $query);
		$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
		$this->playerActive = $row['playerActive'];
		
		return 0;
	}
	function setPlayerTeam($connect)
	{
		$query = "SELECT * FROM playerlist WHERE playerCode='".$this->playerCode."'";
		$result = mysqli_query($connect, $query);
		$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
		$this->playerTeam = $row['playerTeam'];
		
		return 0;
	}
	function setPlayerPosition($connect)
	{
		$query = "SELECT * FROM playerlist WHERE playerCode='".$this->playerCode."'";
		$result = mysqli_query($connect, $query);
		$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
		if ($row["playerPosition"] == NULL)
		{
			echo"<BR>Player ".$code." has invalid position in playerlist database.";
			$error = 1;
		}
		$this->playerPosition = $row['playerPosition'];
		
		return $error;
	}
	function setPlayerValue($connect)
	{
		$error = 0;
		$query = "SELECT * FROM playerlist WHERE playerCode='".$this->playerCode."'";
		$result = mysqli_query($connect, $query);
		$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
		if ($row["playerValue"] == NULL)
		{
			echo"<BR>Player ".$code." has invalid value playerlist database.";
			$error = 1;
		}
		$this->playerValue = $row['playerValue'];
		
		return $error;
	}
	function setPlayerWeekIn($temp)
	{
		if ($temp != NULL)
			$this->playerWeekIn = $temp;
		else
			return 1;
		return 0;
	}
	function setPlayerWeekOut($temp)
	{
		if ($temp != NULL)
			$this->playerWeekOut = $temp;
		else
			return 1;
		return 0;
	}
	
	function getPlayerCode()
	{
		return $this->playerCode;
	}
	function getPlayerName()
	{
		return $this->playerName;
	}
	function getPlayerActive()
	{
		return $this->playerActive;
	}
	function getPlayerTeam()
	{
		return $this->playerTeam;
	}
	function getPlayerPosition()
	{
		return $this->playerPosition;
	}
	function getPlayerValue()
	{
		return $this->playerValue;
	}
	function getPlayerWeekIn()
	{
		return $this->playerWeekIn;
	}
	function getPlayerWeekOut()
	{
		return $this->playerWeekOut;
	}
	function getPlayerLastYearScore()
	{
		return $this->playerLastYearScore;
	}
	function getPlayerScore($week)
	{
		if ($this->playerScore == NULL)
			return 0;
		if ($week == $this->maxWeeks)
		{	// Just to speed up processing time
			return $this->playerScore;
		}
		else
		{
			$playerScore = 0;
			for ($i = 1; $i <= $week; $i++)
			{
				$playerScore += $this->weeklyScore[$i];
			}
			return $playerScore;
		}
	}
	
	function getPlayerScoreCaptains($week, $captainForWeek)
	{
		if ($this->playerScore == NULL)
			return 0;
		
		$playerScore = 0;
		for ($i = 1; $i <= $week; $i++)
		{
			if ($captainForWeek[$i] == 0)
			{
				// This player was not a captain in week $i - checking in the captain array
				$playerScore += $this->weeklyScore[$i];
			}
			else
			{
				$playerScore += ($this->weeklyScore[$i] * 2);
			}
		}
		return $playerScore;
	}

	function getPlayerScoreForWeek($week)
	{
		return $this->weeklyScore[$week];
	}
	
	function getPlayerScoreForWeekCaptains($week, $captainForWeek)
	{
		if ($this->weeklyScore[$week]==0)
		{
			return $this->weeklyScore[$week];
		}
		if ($captainForWeek[$week] == 0)
		{
			// This player was not a captain in week $i - checking in the captain array
			return $this->weeklyScore[$week];
		}
		else
		{
			return $this->weeklyScore[$week] * 2;
		}
	}
 
	function calculatePlayerScore($connect)
	{
		// How players score
		// Players score points from the week before their weekIn value until the week of the weekOut value
		// eg a player with weekIn=1 and weekOut = 1 will not score
		// eg a player with weekIn=1 and weekOut = 2 will score for week 1
		// eg a player with weekIn=1 and weekOut = 3 will score for week 1,2
		// week is pre games for the week
		$query = "SELECT * FROM playerlist WHERE playerCode='".$this->playerCode."'";
		$result = mysqli_query($connect, $query);
		$finfo = mysqli_fetch_fields($result);
		$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
		for ($i = 1; $i <= $this->maxWeeks; $i++)
		{
			if ($i < 10)
				$week = "week0".$i;
			else
				$week = "week".$i;
			
			if ($this->playerWeekOut != NULL)	// This player has been transferred out at some point
			{
				if ( ($i > $this->playerWeekIn) && ($i < $this->playerWeekOut) )	// 
				{
					// Score for this week
					$this->weeklyScore[$i] = $row[$week];
					$this->playerScore += $row[$week];
				}
				else if ( ($i == $this->playerWeekIn) && ($i < $this->playerWeekOut))
				{
					// Score for this week
					$this->weeklyScore[$i] = $row[$week];
					$this->playerScore += $row[$week];
				}
				else
				{
					// Don't score for this week
				}
			}
			else	// This player has not been transferred out
			{
				if ($i >= $this->playerWeekIn)
				{
					// Score for this week
					$this->weeklyScore[$i] = $row[$week];
					$this->playerScore += $row[$week];
				}
				else
				{
					// This player has not been transferred in at this week, don't score for this week
				}
			}
			//echo"<BR>i ".$i." Weekly Score".$this->weeklyScore[$i]." total ".$this->playerScore;
		}
		return $weeklyScore;
	}
	
	function shirtLookup()
	{
		if (!$this->getPlayerActive())
		{	
			// This player is not active, give him a blank shirt so it's obvious when viewed
			return $path = "/JPEGs/Blank.gif";
		}

		switch($this->getPlayerTeam())
		{
			case "ARS"	: 	$path = "/JPEGs/Arsenal.gif";
							break;
			case "AVL"	: 	$path = "/JPEGs/AstonVilla.gif";
							break;
			case "BIR"	: 	$path = "/JPEGs/Birmingham.gif";
							break;
			case "BLA"	:	$path = "/JPEGs/Blackpool.gif";
							break;
			case "BLR"	: 	$path = "/JPEGs/Blackburn.gif";
							break;
			case "BOL"	: 	$path = "/JPEGs/Bolton.gif";
							break;
			case "BUR"	: 	$path = "/JPEGs/Burnley.gif";
							break;
			case "CHA"	: 	$path = "/JPEGs/Charlton.gif";
							break;
			case "DER"	: 	$path = "/JPEGs/Derby.gif";
							break;
			case "CHE"	: 	$path = "/JPEGs/Chelsea.gif";
							break;
			case "EVE"	: 	$path = "/JPEGs/Everton.gif";
							break;
			case "FUL"	: 	$path = "/JPEGs/Fulham.gif";
							break;
			case "HUL"	: 	$path = "/JPEGs/Hull.gif";
							break;
			case "LIV"	: 	$path = "/JPEGs/Liverpool.gif";
							break;
			case "MCY"	:
			case "MAN"	: 	$path = "/JPEGs/ManCity.gif";
							break;
			case "MUN"	: 	$path = "/JPEGs/manUtd.gif";
							break;
			case "MID"	: 	$path = "/JPEGs/Middlesbro.gif";
							break;
			case "NEW"	: 	$path = "/JPEGs/Newcastle.gif";
							break;
			case "NOR"	: 	$path = "/JPEGs/Norwich.gif";
							break;
			case "POR"	: 	$path = "/JPEGs/Portsmouth.gif";
							break;
			case "QPR"	: 	$path = "/JPEGs/QPR.gif";
							break;
			case "REA"	: 	$path = "/JPEGs/Reading.gif";
							break;
			case "SHF"	: 	$path = "/JPEGs/SheffUtd.gif";
							break;
			case "STO"	: 	$path = "/JPEGs/Stoke.gif";
							break;
			case "SUN"	: 	$path = "/JPEGs/Sunderland.gif";
							break;
			case "SWA"	: 	$path = "/JPEGs/Swansea.gif";
							break;
			case "TOT"	: 	$path = "/JPEGs/Tottenham.gif";
							break;
			case "WAT"	: 	$path = "/JPEGs/Watford.gif";
							break;
			case "WBA"	: 	$path = "/JPEGs/WestBrom.gif";
							break;
			case "WHM"	: 	$path = "/JPEGs/WestHam.gif";
							break;
			case "WIG"	: 	$path = "/JPEGs/Wigan.gif";
							break;
			case "WLV"	:
			case "WOL"	: 	$path = "/JPEGs/Wolves.gif";
							break;
			default		: 	$path = "/JPEGs/Blank.gif";
							break;
		}
		return $path;
	}
	
	function loadPlayerOccurrence($connect)
	{
		$cDatabase = new database;
		$activeDatabase = $cDatabase->getActiveDatabase($connect);
		
		$db = $cDatabase->selectDatabase($connect, $activeDatabase);
		
		$queryIn = " SELECT * FROM memberlist where ";
		$queryOut = " SELECT * FROM memberlist where ";
		$queryTotal = " SELECT * FROM memberlist where ";
		
		for ($i = 1; $i <= MAXPLAYERS; $i++)
		{
			$subQueryTotal = getPlayerCodeVariable($i)." = '".$this->playerCode."'";
			if ($i != MAXPLAYERS)
			{
				$subQueryTotal = $subQueryTotal." OR ";
			}
			$queryTotal = $queryTotal.$subQueryTotal;
			
			$subQueryIn = "(".getPlayerCodeVariable($i)." = '".$this->playerCode."' AND ".getPlayerWeekInVariable($i)." >1)";
			if ($i != MAXPLAYERS)
			{
				$subQueryIn = $subQueryIn." OR ";
			}
			$queryIn = $queryIn.$subQueryIn;
			
			$subQueryOut = "(".getPlayerCodeVariable($i)." = '".$this->playerCode."' AND ".getPlayerWeekOutVariable($i)." >0)";
			if ($i != MAXPLAYERS)
			{
				$subQueryOut = $subQueryOut." OR ";
			}
			$queryOut = $queryOut.$subQueryOut;
		}

		$result = mysqli_query($connect, $queryTotal);
		$total = mysqli_num_rows($result);
		$result = mysqli_query($connect, $queryIn);
		$this->In = mysqli_num_rows($result);
		$result = mysqli_query($connect, $queryOut);
		$this->Out = mysqli_num_rows($result);
		$this->Current = $total - $this->Out;
	}
}
?>